# -*- coding: utf-8 -*-

from fastapi import APIRouter, HTTPException, Depends
from sqlalchemy.orm import Session
from database.database import SessionLocal
from crud import ota as ota_crud

# 创建路由
ApiRouter = APIRouter(prefix="/v1", tags=['APP配置'])

def get_db():
    db = SessionLocal()
    try:
        yield db
    finally:
        db.close()

@ApiRouter.get("/app-config", summary="获取APP配置信息")
async def get_app_config(db: Session = Depends(get_db)):
    """客户端获取APP配置信息 - 返回当前使用的TTS和ASR提供商"""
    try:
        # 分别获取CM3和DSP的最新固件版本
        cm3_firmware = ota_crud.get_latest_cm3_firmware(db)
        dsp_firmware = ota_crud.get_latest_dsp_firmware(db)
        
        # 调试信息
        print(f"DEBUG: cm3_firmware = {cm3_firmware}")
        print(f"DEBUG: dsp_firmware = {dsp_firmware}")
        
        if cm3_firmware:
            print(f"DEBUG: cm3_firmware.cm3_version = {cm3_firmware.cm3_version}")
        if dsp_firmware:
            print(f"DEBUG: dsp_firmware.dsp_version = {dsp_firmware.dsp_version}")
        
        # 获取CM3和Flash版本号
        cm3_version = cm3_firmware.cm3_version if cm3_firmware and cm3_firmware.cm3_version else "1.0.0"
        flash_version = dsp_firmware.dsp_version if dsp_firmware and dsp_firmware.dsp_version else "1.0.0"
        
        print(f"DEBUG: 最终cm3_version = {cm3_version}")
        print(f"DEBUG: 最终flash_version = {flash_version}")
        
        # 直接返回配置，不需要数据库操作
        config_data = {
            "tts_provider": "doubao",  # 当前使用的TTS提供商
			
			#"asr_provider": "doubao",  # 当前使用的ASR提供商
            #"server_url": "qixin.yeshan.fun",#"http://127.0.0.1:8000",  # 服务器地址
            "asr_provider": "aliyun",  # 当前使用的ASR提供商
            "server_url": "112.74.97.118",#"http://127.0.0.1:8000",  # 服务器地址
            "app_version": "1.0.0",  # APP版本
            "cm3_version": cm3_version,  # CM3版本 - 从OTA系统获取
            "flash_version": flash_version  # Flash版本 - 从OTA系统获取
        }
        
        return {
            "success": True,
            "data": config_data,
            "message": "获取配置成功"
        }
    except Exception as e:
        print(f"DEBUG: 异常 = {e}")
        raise HTTPException(status_code=500, detail=f"获取配置失败: {str(e)}") 